//package com.haoze.nurseapp.schedule;
//
//import com.haoze.common.response.Result;
//import com.haoze.nurseapp.dao.TaskExecuteMapper;
//import com.haoze.nurseapp.enumeration.WorkSpaceEnum;
//import com.haoze.nurseapp.service.TaskExecuteService;
//import org.apache.commons.lang.StringUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//import org.springframework.transaction.annotation.Transactional;
//
//import javax.annotation.Resource;
//import java.sql.*;
//import java.util.*;
//import java.util.Date;
//
//@Component
//@ConditionalOnProperty(prefix = "schedule", name = "pivas", havingValue = "true")
//public class PivasSchedule {
//    private static final Logger logger = LoggerFactory.getLogger(PivasSchedule.class);
//
//    /**
//     * 配置数据库JDBC连接信息
//     */
//    private static String user = "sa";
//    private static String password = "Jack110110";
//    private static String url = "jdbc:sqlserver://172.20.6.126:1433;DatabaseName=ZHY_PIVAS_CZEY;";
//
//    private int flag = 0;
//
//
//    @Resource
//    TaskExecuteMapper taskExecuteMapper;
//
//    @Resource
//    TaskExecuteService taskExecuteService;
//
//    /**
//     * 每分钟增量同步静配中心配置数据
//     */
//    @Scheduled(cron = "0 0/1 * * * ?")
//    @Transactional(rollbackFor = Exception.class)
//    public void refreshPivasData() {
//        //logger.info("{}", new Date());
//        // 先查询执行记录barcode获取最大值，如果没有则默认同步当前时间一天前所有barcode记录
//        String barcode = taskExecuteMapper.queryLargestBarcode();
//        //String barcode = "20031902833750";//20032402839576 - 1134756 - 58    20032402839578 - 1134756 - 60    20032402839577 - 1134756 - 59
//        String sql = "";
//        /**
//         * 扫描二维码之后需要拿到二维码内的三方barcode去三方sqlserver中查询视图，来确定当前静配药房记录是否已加药
//         */
//        Connection conn = null;
//        Statement stmt = null;
//        ResultSet rs = null;
//        try {
//            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
//            // 连接数据库
//            conn = DriverManager.getConnection(url, user, password);
//            // 建立Statement对象
//            stmt = conn.createStatement();
//            // 执行数据库查询语句
//            if (StringUtils.isBlank(barcode)) {
//                //如果task_execute_detail表中没有barcode数据则扫描静配中心视图最大barcode取其中数据匹配本地库的医嘱编号及组号
//                sql = "SELECT ISNUMERIC(tt.batch) as dosingStatus,tt.visit_id, tt.barcode, tt.batch, tt.order_no, tt.patient_no, tt.patient_id FROM dbo.pat_drug_master tt WHERE tt.barcode = (SELECT MAX(barcode) FROM dbo.pat_drug_master)";
//                rs = stmt.executeQuery(sql);
//                flag = 0;
//            } else {
//                sql = "SELECT TOP 10 ISNUMERIC(a.batch) as dosingStatus,a.visit_id, a.barcode, a.batch, a.order_no, a.patient_no, a.patient_id FROM dbo.pat_drug_master a where barcode > " + barcode +"ORDER BY barcode asc";
//                rs = stmt.executeQuery(sql);
//                flag = 1;
//            }
//
//            queryOneOrder(rs);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            try{
//                if (rs != null) {
//                    rs.close();
//                    rs = null;
//                }
//                if (stmt != null) {
//                    stmt.close();
//                    stmt = null;
//                }
//                if (conn != null) {
//                    conn.close();
//                    conn = null;
//                }
//            } catch (SQLException e){
//                e.printStackTrace();
//            }
//        }
//    }
//
//    private void saveInfo(ResultSet rs) throws SQLException {
//        String dosingStatus = rs.getString("dosingStatus");
//        String backBarcode = rs.getString("barcode");
//        String inpatNum = rs.getString("patient_no") + "-" + rs.getString("visit_id");
//        String orderNo = rs.getString("order_no");
//        String batch = rs.getString("batch");
//        System.out.println("barcode: "+backBarcode+"; "+"inpatNum: "+inpatNum+"; "+"orderNo: "+orderNo+"; "+"batch: "+batch);
//
//        // 根据inpatNum 和 orderNo获取医嘱并获取医嘱关联的qrcode和任务编号
//        Map<String, Object> map = new HashMap<>();
//        String inpatOrderId = taskExecuteMapper.queryInpatOrderId(inpatNum, orderNo);
//        if (StringUtils.isBlank(inpatOrderId)) {
//            //logger.info("未找到{}的对应医嘱{}", inpatNum, orderNo);
//        } else {
//            map.put("batchNum", 1);
//            map.put("inpatOrderId", inpatOrderId);
//            //根据医嘱ID获取当前任务ID及节点ID
//            String taskId = taskExecuteMapper.queryTaskIdByOrderNum(inpatOrderId);
//            String taskNodeId = taskExecuteMapper.queryTaskNodeIdByOrderNum(inpatOrderId);
//            if(!(StringUtils.isBlank(taskId)||StringUtils.isBlank(taskNodeId))){
//                map.put("type", "1");
//                map.put("barcode", backBarcode);
//                map.put("taskId", taskId);
//                map.put("taskNodeId", taskNodeId);
//                map.put("dosingStatus", dosingStatus);
//                //logger.info("map = {}", map);
//                taskExecuteService.createPivasBatchQrcode(map);
//            }
//        }
//
//    }
//
//    private void queryOneOrder(ResultSet rs) throws SQLException {
//        while (rs.next()) {
//            if (0==flag) {
//                String inpatNum = rs.getString("patient_id")+"-1";
//                String orderNo = rs.getString("order_no");
//                //根据病人入院号和医嘱组号获取本地库中的医嘱信息若存在则匹配barCode对应qrCode
//                Integer count = taskExecuteMapper.queryOneInpatOrder(inpatNum,orderNo,WorkSpaceEnum.STATIC_DRUG_CENTER.getNodeId());
//                if(count==0){
//                    return;
//                } else {
//                    saveInfo(rs);
//                }
//            } else {
//                saveInfo(rs);
//            }
//        }
//    }
//
//}
